// pages/shopStore/manaGood.js
var api = require('../../utils/api.js');
var rsa = require('../../utils/rsa.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showcateview:false,
    inpt:'',
    group:[],
    catindex:0,
    goods:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.checkCate();
  },
  //选择分类
  selectcate:function(e){
    var index = e.currentTarget.dataset.info;
    this.setData({
      catindex:index,
    })
    this.checkgood(index);
  },  
  //查询分类
  checkCate:function(){
    const shopNum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'goods.cat_list', "seller_bn": shopNum, 'session_id': settion
    }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000,
        })
        return
      }
      if (res.return_code == '0000') {
        var grp = res.data.list;
        var index = grp[0].cat_id;
        this.setData({
          group: grp,
          catindex:index,
        })
        this.checkgood(index);
      } else {
        wx.showToast({
          title: '查询失败',
          icon: 'none',
          duration: 2000,
        })
      }
    });
  },
  //查询商品
  checkgood:function(e){
    const shopNum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'goods.search', "seller_bn": shopNum, 'session_id': settion,'cat_id':e
    }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000,
        })
        return
      }
      if (res.return_code == '0000') {
        var gd = res.data.list;
        this.setData({
          goods:gd
        })
      } else {
        wx.showToast({
          title: '查询失败',
          icon: 'none',
          duration: 2000,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  addcate:function(){
    var hiddes = this.data.showcateview;
    this.setData({
      showcateview:!hiddes
    })
  },
  inputcate:function(e){
    this.setData({
      inpt:e.detail.value
    })
  },
  //添加类别
  bindcate:function(){
    const vale = this.data.inpt;
    const shopNum = app.globalData.shoopnum;
    const settion = app.globalData.session;
    if(vale == '')return;
    rsa.Cpic_fetchPost(rsa.SERVERPORT, {
      'method': 'goods.add_cat', "seller_bn": shopNum, 'session_id': settion, 'cat_name':vale
    }, (err, res) => {
      if (err) {
        wx.showToast({
          title: err,
          icon:'none',
          duration:2000,
        })
        return
      }
      if (res.return_code == '0000') {
        wx.showToast({
          title: '添加成功',
          icon: 'none',
          duration: 2000,
        })
        var hiddes = this.data.showcateview;
        this.setData({
          showcateview: !hiddes
        })
      } else {
        wx.showToast({
          title: '添加失败',
          icon: 'none',
          duration: 2000,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //添加商品
  addgoodaction:function(){
    wx.navigateTo({
      url: '/pages/shopStore/goodsEdit',
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})